package com.zqc;

import com.zqc.bean.ServiceComponent;
import com.zqc.task.ConsumerTask;
import com.zqc.task.ReportTask;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 客户端启动类
 */
public class ConsumerBootStrap {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();

        // 定时上报
        Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(new ReportTask(), 5, 5, TimeUnit.SECONDS);

        // 线程执行
        ServiceComponent service = context.getBean(ServiceComponent.class);
        Executor executorServiceA = Executors.newFixedThreadPool(10);
        executorServiceA.execute(new ConsumerTask(service, "A"));

        Executor executorServiceB = Executors.newFixedThreadPool(10);
        executorServiceB.execute(new ConsumerTask(service, "B"));

        Executor executorServiceC = Executors.newFixedThreadPool(10);
        executorServiceC.execute(new ConsumerTask(service, "C"));
    }

    /**
     * 客户端配置类
     */
    @Configuration
    @ComponentScan("com.zqc.bean")
    @PropertySource("classpath:dubbo-consumer.properties")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
